package com.sesum;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.widget.Toast;

public class Actions {
	
	static String host = "10.0.2.2";
	
	public static String register(String login, String password, String carRegistration, String state) {
	       JSONObject j = new JSONObject();
	        try {
		        j.put("login", login);
				j.put("password", password);
		        j.put("car_registration", carRegistration);
		        j.put("state", state);
		        
		        
		        String url = "http://"+host+"/alta.php";
		        HttpResponse re = HTTPPoster.doPost(url, j);
		        
		        if (re.getStatusLine().getReasonPhrase().equals("OK"))
		        {
		            return "OK";
		        }else{
		        	return "NOK";
		        }
		        
	        
	        
	        } catch (JSONException e) {
	        	return "NOK";
	        } catch (ClientProtocolException e) {
	        	return "NOK";
			} catch (IOException e) {
				return "NOK";
			}

	}
	
	public static String decline(String login, String password) {
	       JSONObject j = new JSONObject();
	        try {
		        j.put("login", login);
				j.put("password", password);
		        
		        
		        String url = "http://"+host+"/baja.php";
		        HttpResponse re = HTTPPoster.doPost(url, j);
		        
		        if (re.getStatusLine().getReasonPhrase().equals("OK"))
		        {
		            return "OK";
		        }else{
		        	return "NOK";
		        }
		        
	        
	        
	        } catch (JSONException e) {
	        	return "NOK";
	        } catch (ClientProtocolException e) {
	        	return "NOK";
			} catch (IOException e) {
				return "NOK";
			}

	}
	
	public static String updateState(String login, String password, String carRegistration, String state) {
	       JSONObject j = new JSONObject();
	        try {
		        j.put("login", login);
				j.put("password", password);
				j.put("car_registration", carRegistration);
		        j.put("state", state);
		        
		        
		        String url = "http://"+host+"/updatestate.php";
		        HttpResponse re = HTTPPoster.doPost(url, j);
		        
		        if (re.getStatusLine().getReasonPhrase().equals("OK"))
		        {
		            return "OK";
		        }else{
		        	return "NOK";
		        }
		        
	        
	        
	        } catch (JSONException e) {
	        	return "NOK";
	        } catch (ClientProtocolException e) {
	        	return "NOK";
			} catch (IOException e) {
				return "NOK";
			}

	}
	
	public static String changePassword(String login, String oldPassword, String newPassword) {
	       JSONObject j = new JSONObject();
	        try {
		        j.put("login", login);
				j.put("password", newPassword);
		        
		        
		        String url = "http://"+host+"/changepass.php";
		        HttpResponse re = HTTPPoster.doPost(url, j);
		        
		        if (re.getStatusLine().getReasonPhrase().equals("OK"))
		        {
		            return "OK";
		        }else{
		        	return "NOK";
		        }
	        
	        } catch (JSONException e) {
	        	return "NOK";
	        } catch (ClientProtocolException e) {
	        	return "NOK";
			} catch (IOException e) {
				return "NOK";
			}

	}
	
	public static String existLogin(String login) {
	       JSONObject j = new JSONObject();
	        try {
		        j.put("login", login);
		        
		        String url = "http://"+host+"/checklogin.php";
		        HttpResponse re = HTTPPoster.doPost(url, j);
		        
		        if (re.getStatusLine().getReasonPhrase().equals("OK"))
		        {
		        	
		        	BufferedReader reader = new BufferedReader(new InputStreamReader(re.getEntity().getContent(), "UTF-8"));
		        	StringBuilder builder = new StringBuilder();
		        	for (String line = null; (line = reader.readLine()) != null;) {
		        	    builder.append(line).append("\n");
		        	}
		        	JSONTokener tokener = new JSONTokener(builder.toString());
		        	JSONArray finalResult = new JSONArray(tokener);
		        	if (finalResult.length() >= 1){
		        		return "OK";
		        	}else{
		        		return "NOK";
		        	}
		        }else{
		        	return "EXCEPTION";
		        }
	        
	        } catch (JSONException e) {
	        	return "EXCEPTION";
	        } catch (ClientProtocolException e) {
	        	return "EXCEPTION";
			} catch (IOException e) {
				return "EXCEPTION";
			}

	}
	
	public static JSONObject login(String login, String password) {
	       JSONObject j = new JSONObject();
	        try {
		        j.put("login", login);
				j.put("password", password);
		        
		        
		        String url = "http://"+host+"/login.php";
		        HttpResponse re = HTTPPoster.doPost(url, j);
		        
		        if (re.getStatusLine().getReasonPhrase().equals("OK"))
		        {
		        	BufferedReader reader = new BufferedReader(new InputStreamReader(re.getEntity().getContent(), "UTF-8"));
		        	StringBuilder builder = new StringBuilder();
		        	for (String line = null; (line = reader.readLine()) != null;) {
		        	    builder.append(line).append("\n");
		        	}
		        	JSONTokener tokener = new JSONTokener(builder.toString());
		        	JSONArray finalResult = new JSONArray(tokener);
		        	if (finalResult.length() == 1){
		        		JSONObject user = finalResult.getJSONObject(0);
//		        		String loginJSON = user.getString("login");
//		        		String passwordJSON = user.getString("password");
//		        		String car_registrationJSON = user.getString("car_registration");
//		        		String stateJSON = user.getString("state");
		        		return user;
		        		
		        	}
		        	return null;
		        }else{
		        	return null;
		        }
	        
	        } catch (JSONException e) {
	        	return null;
	        } catch (ClientProtocolException e) {
	        	return null;
			} catch (IOException e) {
				return null;
			}

	}
	
	public static String updateCarRegistration(String login, String password, String carRegistration, String state) {
	       JSONObject j = new JSONObject();
	        try {
		        j.put("login", login);
				j.put("password", password);
		        j.put("car_registration", carRegistration);
		        j.put("state", state);
		        
		        
		        String url = "http://"+host+"/updatecarreg.php";
		        HttpResponse re = HTTPPoster.doPost(url, j);
		        
		        if (re.getStatusLine().getReasonPhrase().equals("OK"))
		        {
		            return "OK";
		        }else{
		        	return "NOK";
		        }
	        
	        } catch (JSONException e) {
	        	return "NOK";
	        } catch (ClientProtocolException e) {
	        	return "NOK";
			} catch (IOException e) {
				return "NOK";
			}

	}
	
	public static JSONObject getUserData(String login, String password) {
	       JSONObject j = new JSONObject();
	        try {
		        j.put("login", login);
				j.put("password", password);
		        
		        String url = "http://"+host+"/getuserdata.php";
		        HttpResponse re = HTTPPoster.doPost(url, j);
		        
		        if (re.getStatusLine().getReasonPhrase().equals("OK"))
		        {
		        	
		        	BufferedReader reader = new BufferedReader(new InputStreamReader(re.getEntity().getContent(), "UTF-8"));
		        	StringBuilder builder = new StringBuilder();
		        	for (String line = null; (line = reader.readLine()) != null;) {
		        	    builder.append(line).append("\n");
		        	}
		        	JSONTokener tokener = new JSONTokener(builder.toString());
		        	JSONArray finalResult = new JSONArray(tokener);
		        	if (finalResult.length() == 1){
		        		JSONObject user = finalResult.getJSONObject(0);
		        		String loginJSON = user.getString("login");
		        		String passwordJSON = user.getString("password");
		        		String car_registrationJSON = user.getString("car_registration");
		        		String stateJSON = user.getString("state");
		        		return user;
		        		
		        	}
		        	return null;
		        }else{
		        	return null;
		        }
	        
	        } catch (JSONException e) {
	        	return null;
	        } catch (ClientProtocolException e) {
	        	return null;
			} catch (IOException e) {
				return null;
			}

	}
	
//	public String addCarRegistration(String login, String password, String carRegistration, String state) {
//	       JSONObject j = new JSONObject();
//	        try {
//		        j.put("login", login);
//				j.put("password", password);
//		        j.put("car_registration", carRegistration);
//		        j.put("state", state);
//		        
//		        
//		        String url = "http://"+host+"/addcarregistration.php";
//		        HttpResponse re = HTTPPoster.doPost(url, j);
//		        
//		        if (re.getStatusLine().getReasonPhrase().equals("OK"))
//		        {
//		            return "OK";
//		        }else{
//		        	return "NOK";
//		        }
//	        
//	        } catch (JSONException e) {
//	        	return "NOK";
//	        } catch (ClientProtocolException e) {
//	        	return "NOK";
//			} catch (IOException e) {
//				return "NOK";
//			}
//
//	}

}
